package io.renren.modules.merchant.service.impl;

import io.renren.modules.merchant.dao.MerchantStatisticsDao;
import io.renren.modules.merchant.dao.ReportDao;
import io.renren.modules.merchant.entity.TurnoverVO;
import io.renren.modules.merchant.service.MerchantService;
import io.renren.modules.merchant.service.ReportService;
import io.renren.modules.order.dao.MerchantOrderDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static io.renren.common.utils.ShiroUtils.getUserId;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportDao reportDao;

    @Autowired
    private MerchantService merchantService;

    @Autowired
    private MerchantStatisticsDao merchantStatisticsDao;
    /**
     * 区间日期的营业额统计
     * @param begin
     * @param end
     * @return
     */
    public TurnoverVO getTurnoverStatistics(LocalDate begin, LocalDate end) {


        Long userId = getUserId();
        String merchantId = merchantService.getMerchantByUserId(userId).getMerchantId();


        List<LocalDate> dateList =new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        List<Double> turnoverList = new ArrayList<>();
        for (LocalDate date : dateList) {



            Map map = new HashMap();
            map.put("day",date);
            Double turnover = merchantStatisticsDao.getTurnoverByDate(map);
            turnover = turnover == null ? 0.0 : turnover;
            turnoverList.add(turnover);
        }
        TurnoverVO turnoverVO =new TurnoverVO();


        turnoverVO.setTurnoverList(StringUtils.join(turnoverList,","));
        turnoverVO.setDateList(StringUtils.join(dateList,","));




        return turnoverVO;
    }
}
